package com.litree.service.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.litree.service.model.CollectionBean;
import com.litree.service.services.ICollectionSerivce;
import com.litree.service.utils.CommonUtils;
import com.litree.service.utils.Crypt;
import com.litree.service.utils.ReturnMSGUtils;
import com.litree.service.utils.ReturnMsg;



/**
 * 
 * @author GM
 *
 */
@SuppressWarnings("rawtypes")
@Controller
public class CollectionController extends BaseController{
private static final Logger logger = LoggerFactory.getLogger(CollectionController.class);

@Autowired
private ICollectionSerivce service;

/**
 * 获取收藏列表信息接口
 * @param searchKey
 * @param response
 */
@RequestMapping(method=RequestMethod.GET, value="/collectionInfo/{pageSize}/{pageNo}")
public void getCollectionInfo(@PathVariable int pageSize,@PathVariable int pageNo,
		HttpServletResponse response,HttpServletRequest request ) {
	try{			
		Map tokenMap = (Map) request.getAttribute("tokenMap");
		String loginName = (String) tokenMap.get("loginName");
		List dataList = service.getCollectionInfo(loginName,pageSize,pageNo);
		Long totalCount = service.getCollectionInfoCount(loginName);
		if(!dataList.isEmpty()&&dataList.size()>0){
			ReturnMSGUtils.sentMsg(response, dataList.size(), ReturnMsg.OPTION_SUCCESS, ReturnMsg.OPTION_SUCCESS_DESC,"dataList",dataList,"totalresult",totalCount);
		}else{
			ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.NO_SELECTED_RESLUTS,ReturnMsg.NO_SELECTED_RESLUTS_DESC);
		}
	}catch(Exception e){
		try {
			ReturnMSGUtils.sentMsgWithCause(response, "0", ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e.toString());
			logger.error(CommonUtils.errMsg(ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}

/**
 * 添加收藏信息接口
 * @param bodyStr
 * @param response
 */
@RequestMapping(method=RequestMethod.POST, value="/collectionInfo")
public void getCollectionInfo(@RequestBody String bodyStr,
		HttpServletResponse response,HttpServletRequest request ) {
	try{			
		CollectionBean collectionBean = JSON.parseObject(bodyStr, CollectionBean.class);
		Map tokenMap = (Map) request.getAttribute("tokenMap");
		String loginName = (String) tokenMap.get("loginName");
		collectionBean.setLoginName(loginName);
		Integer backInt = service.setCollectionInfo(collectionBean);
		if(backInt==1){
			ReturnMSGUtils.sentMsg(response, String.valueOf(backInt), ReturnMsg.OPTION_SUCCESS, ReturnMsg.OPTION_SUCCESS_DESC);				
		}else{
			ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.INSERT_ERROR, ReturnMsg.INSERT_ERROR_DESC);
		}
	}catch(Exception e){
		try {
			ReturnMSGUtils.sentMsgWithCause(response, "0", ReturnMsg.INSERT_ERROR, ReturnMsg.INSERT_ERROR_DESC, e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		logger.error(CommonUtils.errMsg(ReturnMsg.INSERT_ERROR, ReturnMsg.INSERT_ERROR_DESC, e));
	}
}


/**
 * 水站是否已经收藏接口
 * @param stationId
 * @param response
 */
@RequestMapping(method=RequestMethod.GET, value="/collectionInfo/isCollected/{stationId}")
public void checkCollectionStationInfo(@PathVariable long stationId,
		HttpServletResponse response,HttpServletRequest request ) {
	try{			
		Map tokenMap = (Map) request.getAttribute("tokenMap");
		String loginName = (String) tokenMap.get("loginName");
		boolean flg = service.checkCollectionStationInfo(loginName,stationId);
		if(flg){//已收藏
			ReturnMSGUtils.sentMsg(response, "1", ReturnMsg.OPTION_SUCCESS, "该水站已收藏过");
		}else{//未收藏
			ReturnMSGUtils.sentMsg(response, "0", ReturnMsg.OPTION_SUCCESS , "该水站未被收藏过");
		}
	}catch(Exception e){
		try {
			ReturnMSGUtils.sentMsgWithCause(response, "0", ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e.toString());
			logger.error(CommonUtils.errMsg(ReturnMsg.SELECT_ERROR, ReturnMsg.SELECT_ERROR_DESC, e));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
}

}
